<?php
/**
 * JS_API支付demo
 * ====================================================
 * 在微信浏览器里面打开H5网页中执行JS调起支付。接口输入输出数据格式为JSON。
 * 成功调起支付需要三个步骤：
 * 步骤1：网页授权获取用户openid
 * 步骤2：使用统一支付接口，获取prepay_id
 * 步骤3：使用jsapi调起支付
*/
	include_once("../WxPayPubHelper/WxPayPubHelper.php");

//	$xml = $GLOBALS['HTTP_RAW_POST_DATA'];

	if (!isset($_GET['horder_id']))
	{
		//触发微信返回code码
		$url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL_HORDER);
		Header("Location: $url"); 
	}else
	{
	        $horder_id = $_GET['horder_id'];

		$con = mysql_connect("localhost","root","3113110");

		if (!$con){
		  die('Could not connect: ' . mysql_error());
		}
		
		mysql_select_db("tianshihui", $con);

		mysql_query("set character set 'utf8'");	

		$result = mysql_query(sprintf("SELECT * FROM tbl_hotel_order where id=%d",$horder_id));
		
		$row = mysql_fetch_assoc($result);
		$price = (int) $row['price'] * 100;	
		$ucode = $row['code'];	
		mysql_close($con);
	}


	

	//使用jsapi接口
	$jsApi = new JsApi_pub();

	//=========步骤1：网页授权获取用户openid============
	//通过code获得openid
	if (!isset($_GET['code']))
	{
		//触发微信返回code码
		$url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL_HORDER);
		Header("Location: $url"); 
	}else
	{
		//获取code码，以获取openid
	        $code = $_GET['code'];
		$jsApi->setCode($code);
		$openid = $jsApi->getOpenId();
	}
	
	//=========步骤2：使用统一支付接口，获取prepay_id============
	//使用统一支付接口
	$unifiedOrder = new UnifiedOrder_pub();
	
	//设置统一支付接口参数
	$unifiedOrder->setParameter("openid","$openid");//商品描述
	$unifiedOrder->setParameter("body","中国青年天使会酒店预订");//商品描述
	//自定义订单号，此处仅作举例
	$timeStamp = time();
	$out_trade_no = WxPayConf_pub::APPID."$timeStamp";
	$unifiedOrder->setParameter("out_trade_no","$ucode");//商户订单号 
	//$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 
	$unifiedOrder->setParameter("total_fee",$price);//总金额
	$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_HOTEL_URL);//通知地址 
	$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
	//非必填参数，商户可根据实际情况选填
	$prepay_id = $unifiedOrder->getPrepayId();

	//=========步骤3：使用jsapi调起支付============
	$jsApi->setPrepayId($prepay_id);

	$jsApiParameters = $jsApi->getParameters();
?>

<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <title>微信安全支付</title>

	<script type="text/javascript">
		//调用微信JS api 支付
		function jsApiCall()
		{
			WeixinJSBridge.invoke(
				'getBrandWCPayRequest',
				<?php echo $jsApiParameters; ?>,
				function(res){
					WeixinJSBridge.log(res.err_msg);
					// alert(res.err_code+res.err_desc+res.err_msg);
					if(res.err_msg == 'get_brand_wcpay_request:ok'){
						// 支付成功   调用成功页面
						
					}
				}
			);
		}

		function callpay()
		{
			if (typeof WeixinJSBridge == "undefined"){
			    if( document.addEventListener ){
			        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			    }else if (document.attachEvent){
			        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
			        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
			    }
			}else{
			    jsApiCall();
			}
		}
		window.onload=function()//用window的onload事件，窗体加载完毕的时候
		{
		   callpay();
		}
	</script>
</head>
<body>
	<a href="http://tianshihui.eventool.cn/index.php/bfront/index">返回首页</a>
</body>
</html>
